package com.bupt.liutong.admin.core.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bupt.liutong.admin.core.base.Constants;
import com.bupt.liutong.admin.core.dao.CommonDao;
import com.bupt.liutong.admin.core.dto.Tree;
import com.bupt.liutong.admin.core.dto.TreeCategory;
import com.bupt.liutong.admin.core.dto.TreeConfig;
import com.bupt.liutong.admin.core.dto.TreeNode;
import com.bupt.liutong.admin.core.dto.TreeParser;
import com.bupt.liutong.admin.core.dto.TreeRoot;
import com.bupt.liutong.admin.core.exception.TreeDefNotFoundException;
import com.bupt.liutong.admin.core.form.TreeForm;
import com.bupt.liutong.admin.core.form.TreeNodeForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.FileUtils;

public class TreeBo {
	private CommonDao commonDao;
	private final TreeConfig treeConfig = TreeParser
			.loadConfig(FileUtils.getProjectPath() + "admin/core/tree/tree.xml");
	
	public TreeForm getCommonTree(String treeId) throws TreeDefNotFoundException {
		// 取得弹出树形菜单的配置参数
		List<Tree> treeList = treeConfig.getTreeList();
		Tree tree = null;
		for(int i=0; i<treeList.size(); i++) {
			tree = treeList.get(i);
			if(tree.getId().equals(treeId)){
				break;
			}
		}
		if (tree == null)
			throw new TreeDefNotFoundException();
		
		TreeForm treeForm = new TreeForm();
		BeanUtils.copyProperties(treeForm, tree);
		return treeForm;
	}
	
	public List<TreeNodeForm> dispCommonTree(String treeId, Object sqlParam) throws TreeDefNotFoundException {
		// 取得弹出树形菜单的配置参数
		List<Tree> treeList = treeConfig.getTreeList();
		Tree tree = null;
		for (int i = 0; i < treeList.size(); i++) {
			tree = treeList.get(i);
			if (tree.getId().equals(treeId)) {
				break;
			}
		}
		if(tree==null)
			throw new TreeDefNotFoundException();
		
		// 读取出根节点、中间节点、叶子节点的配置
		TreeRoot treeRoot = tree.getTreeRoot();
		TreeCategory treeCategory = tree.getTreeCategory();
		//TreeNode treeNodeConfig = tree.getTreeNode();

		// 取得菜单节点的列表
		@SuppressWarnings("unchecked")
		List<TreeNode> treeNodeList = (List<TreeNode>) commonDao.getList("Tree."+tree.getId(), sqlParam);
		//for (int i = 0; i < treeNodeList.size(); i++) {
		//	if (treeNodeList.get(i).getEnabled() == 0)
		//		treeNodeList.remove(i);
		//}

		// 存储所有非叶子节点Id
		Map<Integer, ?> parentMap = this.addParentId(treeNodeList);
		
		// 给没有Url的节点设置Url
		List<TreeNodeForm> treeNodeFormList = new ArrayList<TreeNodeForm>();
		for(int i=0; i<treeNodeList.size(); i++){
			TreeNode treeNode = treeNodeList.get(i);
			TreeNodeForm treeNodeForm = new TreeNodeForm();
			BeanUtils.copyProperties(treeNodeForm, treeNode);
			
//			if (StringUtils.isEmpty(treeNodeForm.getUrl())
//					&& (!StringUtils.isEmpty(treeRoot.getUrl())
//							|| !StringUtils.isEmpty(treeCategory.getUrl()) 
//							|| !StringUtils.isEmpty(treeNodeConfig.getUrl()))) {
				if(parentMap.containsKey(treeNodeForm.getId())){
					// 根节点
					if(treeNodeForm.getId() == Constants.ROOT_NODE_ID){
						treeNodeForm.setUrl(treeRoot.getUrl());
						treeNodeForm.setNodeType(TreeNodeForm.ROOT_NODE);
					}
					// 非根非叶子节点
					else {
						treeNodeForm.setUrl(treeCategory.getUrl());
						treeNodeForm.setNodeType(TreeNodeForm.NONROOT_NONLEAVE_NODE);
					}
				}
				// 叶子节点
				else {
					treeNodeForm.setUrl(treeNode.getUrl());
					treeNodeForm.setNodeType(TreeNodeForm.LEAVE_NODE);
				}
//			}
			treeNodeFormList.add(treeNodeForm);
		}
		return treeNodeFormList;
	}
	
	private Map<Integer, ?> addParentId(List<TreeNode> treeNodeList){
		Map<Integer, ?> map = new HashMap<Integer, Object>();
		for (TreeNode treeNode : treeNodeList) {
			if (!map.containsKey(treeNode.getParentId())) {
				map.put(treeNode.getParentId(), null);
			}
		}
		return map;
	}
	
//	private boolean hasSubNode(List<TreeNode> treeNodeList){
//		return false;
//	}
	
	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}
}
